/**

 @Name：CLTPHP 管理员
 @Author：chichu
 @Site：http://www.cltphp.com/admin/
 @License：LPPL
 */
layui.define(['admin', 'table', 'util','form'], function (exports) {
    var $ = layui.$, admin = layui.admin, table = layui.table, element = layui.element,form = layui.form;
    //全部数据
    tableIn = table.render({
        elem: '#list',
        url: '/admin/admin/index',
        method: 'post',
        toolbar: '#toolbar',
        title:'管理员列表',
        cols: [[
            {type: "checkbox", fixed: true}
            ,{field:'username', title: '用户名', width:150,fixed: true}
            ,{field:'email', title: '邮箱', width:200}
            ,{field:'title', title: '用户组', width:200}
            ,{field:'tel',title: '电话', width:150, hide:true,}
            ,{field:'logintime',title: '登录日期', width:200}
            ,{field:'ip', title: '登录IP',width:150}
            ,{field:'is_open', title: '状态',width:150,toolbar: "#open"}
            ,{width:160, align:'center', toolbar: "#action"}
        ]]
    });

    //改变管理员状态
    form.on('switch(open)', function(obj){
        var id = this.value;
        var is_open = obj.elem.checked===true?1:0;
        admin.req({
            url: '/admin/admin/open'
            , data: {'admin_id':id,'is_open':is_open}
            ,type:'post'
            , success: function (res) {
                if(res.code>0){
                    if(obj.elem.checked==true){
                        $(obj.elem).prop('checked',false);
                    }else{
                        $(obj.elem).prop('checked',true);
                    }
                    form.render();
                }else{
                    layer.msg(res.msg,{time:1000,icon:1});
                }
            }
        });
    });
    //监听工具条
    table.on('tool(list)', function(obj){
        var data = obj.data;
        var id = data.admin_id;
        if(obj.event === 'del'){
            layer.confirm('您确定要删除该管理员吗？', function(){
                admin.req({
                    url: '/admin/admin/del/'
                    , type: 'post'
                    , data: {'admin_id':id}
                    , success: function (res) {
                        layer.closeAll('dialog');
                        if(res.code==0){
                            layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        }
                    }
                });
            });
        } else if(obj.event === 'edit'){
            var index = layer.open({
                type: 2,
                content: '/admin/admin/edit/id/'+id,
                area: ['300px', '300px'],
                maxmin: true
            });
            layer.full(index);
        }
    });
    //编辑提交
    form.on('submit(edit-btn)', function(data){
        admin.req({
            url: '/admin/admin/edit/' //实际使用请改成服务端真实接口
            , type: 'post'
            , data: data.field
            , done: function (res) {
                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                });
            }
        });
    });
    //头工具栏事件
    table.on('toolbar(list)', function(obj){
        switch(obj.event){
            case 'add':
                var index = layer.open({
                    type: 2,
                    content: '/admin/admin/add/',
                    area: ['300px', '300px'],
                    maxmin: true
                });
                layer.full(index);
                break;
            case 'direct':
                var checkStatus = table.checkStatus('list'),data = checkStatus.data; //获得选中的数据
                if(data.length === 0) return layer.msg('未选中行',{icon:2});
                var ids = [];
                $.each(data, function(i,val){
                    ids[i] = val['admin_id'];
                });
                layer.confirm('确定删除所选管理员吗？', function(){
                    admin.req({
                        url: '/admin/admin/delAll/'
                        , type: 'post'
                        , data: {ids:ids}
                        , success: function (res) {
                            layer.closeAll('dialog');
                            if(res.code==0){
                                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                    tableIn.reload();
                                });
                            }
                        }
                    });
                });
                break;
        }
    });
    //添加提交
    form.on('submit(add-btn)', function(data){
        admin.req({
            url: '/admin/admin/add/' //实际使用请改成服务端真实接口
            , type: 'post'
            , data: data.field
            , done: function (res) {
                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                });
            }
        });
    });

    exports('admin_user', {});
});